//
//  UIViewController+ICENoneData.m
//  DemoProduct
//
//  Created by ttouch on 16/1/25.
//  Copyright © 2016年 iCE. All rights reserved.
//

#import "UIViewController+Utils.h"
#import "JGProgressHUD.h"

@implementation UIViewController (Utils)

#pragma mark - JGProgressHUD

/// 提示请稍后...HUD
- (void)showTextHUDWithPleaseWait {
    [self showTextHUDWithStr:@"请稍后..."];
}

/// 提示strHUD
- (void)showTextHUDWithStr:(NSString *)text {
    [self hideTextHud];
    
    JGProgressHUD *HUD = [self getJGProgressHUD];
    HUD.textLabel.text = text;
    HUD.userInteractionEnabled = YES;
    HUD.position = JGProgressHUDPositionCenter;
    HUD.marginInsets = (UIEdgeInsets) {
        .top = 0.0f,
        .bottom = 20.0f,
        .left = 10.0f,
        .right = 10.0f,
    };
    HUD.tag = 1111;
    [HUD showInView:self.view];
}

/// 提示操作成功
- (void)showSuccessHUDWithStr:(NSString *)text {
    [self showSuccessHUDWithStr:text duration:2.0];
}

- (void)showSuccessHUDWithStr:(NSString *)text duration:(CGFloat)duration {
    [self hideTextHud];
    
    JGProgressHUD *HUD = [self getJGProgressHUD];
    HUD.textLabel.text = text;
    HUD.indicatorView = [[JGProgressHUDSuccessIndicatorView alloc] init];
    HUD.square = YES;
    [HUD showInView:[UIViewController currentViewController].view];
    [HUD dismissAfterDelay:duration];
}

/// 提示操作失败
- (void)showErrorHUDWithError:(NSString *)text {
    [self hideTextHud];
    
    JGProgressHUD *HUD = [self getJGProgressHUD];
    HUD.textLabel.text = text;
    HUD.indicatorView = [[JGProgressHUDErrorIndicatorView alloc] init];
    HUD.square = YES;
    [HUD showInView:[UIViewController currentViewController].view];
    [HUD dismissAfterDelay:2.5];
}

/// 提示操作失败
- (void)showErrorTipHUDWithError:(NSString *)text {
    [self hideTextHud];
    
    JGProgressHUD *HUD = [self getJGProgressHUD];
    HUD.textLabel.text = text;
    HUD.indicatorView = [[JGProgressHUDImageIndicatorView alloc] initWithImage:[UIImage imageNamed:@"Shape"]];
    HUD.square = YES;
    [HUD showInView:[UIViewController currentViewController].view];
    [HUD dismissAfterDelay:2.5];
}

/// 隐藏HUD
- (void)hideTextHud {
//    [self hideHud];

    UIView *HUD = [self.view viewWithTag:1111];
    if (HUD) {
        [HUD removeFromSuperview];
    }
    UIView *HUD1 = [self.navigationController.view viewWithTag:1111];
    if (HUD1) {
        [HUD1 removeFromSuperview];
    }
}

- (JGProgressHUD *)getJGProgressHUD {
    
    JGProgressHUD *HUD = [[JGProgressHUD alloc] initWithStyle:JGProgressHUDStyleDark];
    HUD.animation = [JGProgressHUDFadeZoomAnimation animation];
    HUD.userInteractionEnabled = NO;
    return HUD;
}

#pragma mark - UIActionSheet

/// 显示一个actionSheet
- (void)showActionSheetWithTitle:(NSString *)title btnTitles:(NSArray *)btnTitles btnClickBlock:(void (^)(NSInteger index))btnClickBlock {
    if (btnTitles.count <= 0) {
        return;
    }
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:@"取消"  destructiveButtonTitle:nil otherButtonTitles:nil, nil];
    for (NSInteger i = 0; i < btnTitles.count; i++) {
        [sheet addButtonWithTitle:btnTitles[i]];
    }
    [sheet.rac_buttonClickedSignal subscribeNext:^(NSNumber *x) {
        if (btnClickBlock) {
            btnClickBlock(x.integerValue);
        }
    }];
    [sheet showInView:self.view];
}

- (void)showActionSheetWithTitle:(NSString *)title btnTitleStr:(NSString *)btnTitleStr btnClickBlock:(void (^)(NSInteger index))btnClickBlock {
    NSArray *btnTitls = [btnTitleStr componentsSeparatedByString:@","];
    [self showActionSheetWithTitle:title btnTitles:btnTitls btnClickBlock:btnClickBlock];
}

#pragma mark - 其他

/// 缩放图片
- (UIImage *)scaleFromImage:(UIImage *)image toSize:(CGSize)size {
    
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

/// 获取当前正在显示的ViewController
+ (UIViewController *)currentViewController {
    
    UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    return [rootVc findBestViewController:rootVc];
}

/// 获取当前正在显示的ViewController
- (UIViewController *)currentViewController {
    
    UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    return [self findBestViewController:rootVc];
}

- (UIViewController *)findBestViewController:(UIViewController *)vc {
    if (vc.presentedViewController) {
        // Return presented view controller
        return [self findBestViewController:vc.presentedViewController];
    } else if ([vc isKindOfClass:[UISplitViewController class]]) {
        // Return right hand side
        UISplitViewController *svc = (UISplitViewController *)vc;
        if (svc.viewControllers.count > 0)
            return [self findBestViewController:svc.viewControllers.lastObject];
        else
            return vc;
    } else if ([vc isKindOfClass:[UINavigationController class]]) {
        // Return top view
        UINavigationController *svc = (UINavigationController *)vc;
        if (svc.viewControllers.count > 0)
            return [self findBestViewController:svc.topViewController];
        else
            return vc;
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        // Return visible view
        UITabBarController *svc = (UITabBarController *)vc;
        if (svc.viewControllers.count > 0)
            return [self findBestViewController:svc.selectedViewController];
        else
            return vc;
    } else {
        // Unknown view controller type, return last child view controller
        return vc;
    }
}

/// 显示无数据tipView
- (void)showNoData {
    [self showNoDataWithimageName:nil tipText:@"暂无相关内容"];
}

- (void)showNoDataWithTipText:(NSString *)tipText {
    [self showNoDataWithimageName:nil tipText:tipText];
}

/// 显示不固定的图片
- (void)showNoDataWithimageName:(NSString*)name tipText:(NSString *)tipText{
    
    [self hideNoData];
    
    NSString *imageName = name == nil ? @"nodata":name;
    
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.tag = 1234;
    imgView.frame = CGRectMake(mScreenWidth / 2 - 80, self.view.height / 2 - 110, 160, 160);
    imgView.contentMode = UIViewContentModeCenter;
    imgView.image = [UIImage imageNamed:imageName];
    [self.view addSubview:imgView];
    [self.view bringSubviewToFront:imgView];
    
    UILabel *lable = [[UILabel alloc] init];
    lable.text = tipText;
    lable.tag = 12345;
    lable.frame = CGRectMake(30, self.view.height / 2, mScreenWidth - 60, 30);
    lable.textAlignment = NSTextAlignmentCenter;
    lable.textColor = HJColorRGB(198);
    lable.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:lable];
    [self.view bringSubviewToFront:lable];
}

/// 隐藏无数据tipView
- (void)hideNoData {
    if ([self.view viewWithTag:1234]) {
        [[self.view viewWithTag:1234] removeFromSuperview];
    }
    if ([self.view viewWithTag:12345]) {
        [[self.view viewWithTag:12345] removeFromSuperview];
    }
}
@end
